/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Bean.PaymentBean;
import com.webaccess.retailws.Bean.ProductBean;
import com.webaccess.retailws.Bean.UserAddressBean;
import com.webaccess.retailws.Bean.UserInfoBean;
import com.webaccess.retailws.Delegate.PaymentDelegate;
import com.webaccess.retailws.Delegate.ProductDelegate;
import com.webaccess.retailws.Delegate.UserInfoDelegate;
import com.webaccess.retailws.Util.CommonUtil;
import com.webaccess.retailws.Util.PaymentUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author Owner
 */
public class PaymentDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    ActionMessages errors = null;
    CommonUtil commUtil = null;
    PaymentUtil payUtil = null;
    PaymentBean payBean = null;
    UserInfoBean userBean = null;
    ProductBean prodBean = null;
    ProductDelegate prodDelegate = null;
    PaymentDelegate payDelegate = null;
    UserInfoDelegate userDelegate = null;
    UserAddressBean userAddrBean = null;

    public ActionForward showPaymentDelivery(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        payBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        commUtil = new CommonUtil();
        HttpSession loginSession = request.getSession(true);
        Long cartId = 0L;
        try {

            commUtil.getTopRightListMap(request);
            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());

            cartId = payDelegate.getCartId(userId, 0L);
            payBean = payDelegate.getPaymentDelivery(cartId);
            request.setAttribute("paydelDetail", payBean);

            SUCCESS = "paydelshow";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward confirmPaymentDelivery(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil = new CommonUtil();
        payUtil = new PaymentUtil();
        errors = new ActionErrors();
        HttpSession loginSession = request.getSession(true);
        String paymentType = "";
        Long paymentId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
            DynaActionForm payDelForm = (DynaActionForm) form;
            paymentType = String.valueOf(payDelForm.get("paymentType"));

            if (paymentType.equalsIgnoreCase("") || paymentType == null) {
                errors.add("errPaymentDelivery", new ActionMessage("errors.peydel.required"));
            } else {
                paymentId = payUtil.savePaymentDelivery(paymentType, userId);
                if (paymentId > 0) {
                    loginSession.setAttribute("paymentId", paymentId);
                } else {
                    errors.add("errPaymentDelivery", new ActionMessage("errors.peydel.failure"));
                }
            }
            saveErrors(request, errors);
            if (errors.isEmpty()) {
                SUCCESS = "savepaydelsuccess";
            } else {
                SUCCESS = "savepaydelfailure";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        payBean = new PaymentBean();
        userBean = new UserInfoBean();
        payDelegate = new PaymentDelegate();
        userDelegate = new UserInfoDelegate();
        prodDelegate = new ProductDelegate();
        commUtil = new CommonUtil();
        HttpSession loginSession = request.getSession(true);
        Long cartId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
            Long paymentId = Long.parseLong(loginSession.getAttribute("paymentId").toString());
            userBean = userDelegate.getUserInfo(userId, roleId);
            UserAddressBean[] userAddrList = userDelegate.getUserAddress(userId);
            request.setAttribute("userInfo", userBean);
            request.setAttribute("userAddressInfo", userAddrList);

            cartId = payDelegate.getCartId(userId, 0L);
            ProductBean[] prodCartList = prodDelegate.getAllCartProduct(cartId);
            if (prodCartList != null) {
                request.setAttribute("cartId", cartId);
                request.setAttribute("productCartList", prodCartList);
            }

            payBean = payDelegate.getPaymentDelivery(cartId);
            Long paymentType = payDelegate.getPaymentType(paymentId);
            payBean.setPaymentType(String.valueOf(paymentType));
            request.setAttribute("paydelDetail", payBean);

            if (paymentType == 1) {
                request.setAttribute("paymentType", "Debit Card and Netbanking Including Mastercard, Visa.");
            } else if (paymentType == 2) {
                request.setAttribute("paymentType", "For Net Banking With : ICICI,HDFC,SBI.");
            } else if (paymentType == 3) {
                request.setAttribute("paymentType", "Cash On Delivery.");
            }

            Long orderId = payDelegate.saveOrder(userId, paymentId, cartId, payBean.getPaydelTotal());
            if (orderId > 0) {
                SUCCESS = "ordershow";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showUserOrders(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        payBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        commUtil = new CommonUtil();
        HttpSession loginSession = request.getSession(true);
        Long cartId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
            if (userId > 0 && roleId > 0) {
                PaymentBean[] userOrderList = payDelegate.getUserOrders(userId);
                request.setAttribute("userOrders", userOrderList);
            }
            SUCCESS = "userordershow";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showManageOrders(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        payBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        commUtil = new CommonUtil();
        try {
            commUtil.getTopRightListMap(request);

            request.setAttribute("orderStatusMap", payDelegate.getOrderStatusLHMap());
            PaymentBean[] ordersList = payDelegate.getAllOrders();
            request.setAttribute("ordersList", ordersList);

            SUCCESS = "manageordershow";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward getOrderDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        PaymentBean payOrderBean = new PaymentBean();
        PaymentBean paymentDetailBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        prodDelegate = new ProductDelegate();
        prodBean = new ProductBean();
        commUtil = new CommonUtil();
        HttpSession loginSession = request.getSession(true);
        Long cartId = 0L, cartTotal = 0L;
        try {
            commUtil.getTopRightListMap(request);
            Long orderId = Long.parseLong(request.getParameter("orderId"));
            payOrderBean = payDelegate.getOrderDetail(orderId);
            request.setAttribute("orderDetail", payOrderBean);

            cartId = payDelegate.getCartId(0L, orderId);
            ProductBean[] prodCartList = prodDelegate.getAllCartProduct(cartId);
            if (prodCartList != null) {
                for (int i = 0; i <= prodCartList.length - 1; i++) {
                    cartTotal = Long.parseLong(prodCartList[i].getProductSubTotal()) + cartTotal;
                }
                request.setAttribute("cartId", cartId);
                request.setAttribute("productCartList", prodCartList);
                request.setAttribute("cartTotal", cartTotal);
            }

            paymentDetailBean = payDelegate.getPaymentDelivery(cartId);
            request.setAttribute("orderPayment", paymentDetailBean);

            SUCCESS = "orderdetailshow";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward placeOrder(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil = new CommonUtil();
        payBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        HttpSession loginSession = request.getSession(true);
        try {
            commUtil.getTopRightListMap(request);

            request.setAttribute("bankList", payDelegate.getBankLHMap());
            request.setAttribute("cardTypeList", payDelegate.getCardTypeLHMap());

            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
            Long paymentId = Long.parseLong(loginSession.getAttribute("paymentId").toString());
            Long cartId = payDelegate.getCartId(userId, 0L);
            payBean = payDelegate.getOrder(userId, paymentId, cartId);
            request.setAttribute("order", payBean);
//            request.setAttribute("orderNo", payBean.getOrderId());
//            Long paymentAmt = Long.parseLong(payBean.getPaydelTotal());
//            request.setAttribute("paymentAmount", paymentAmt);

            Long paymentType = payDelegate.getPaymentType(paymentId);
            if (paymentType == 1) {
                SUCCESS = "showpaymentcard";
            } else if (paymentType == 2) {
                SUCCESS = "showpaymentbank";
            } else if (paymentType == 3) {
                SUCCESS = "placeordersuccess";
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward updateOrderStatus(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil = new CommonUtil();
        payBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        HttpSession loginSession = request.getSession(true);
        try {
            commUtil.getTopRightListMap(request);
            Long orderId = Long.parseLong(request.getParameter("orderId"));
            Long orderStatusId = Long.parseLong(request.getParameter("orderStatusId"));
            Long cartId = payDelegate.getCartId(0L, orderId);
            boolean isOrderStatusUpdated = payDelegate.updateOrderStatus(orderId, orderStatusId,cartId);
            if(isOrderStatusUpdated == true){
                SUCCESS = "updateorderstatussuccess";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showOrderConfirm(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        payDelegate = new PaymentDelegate();
        payUtil = new PaymentUtil();
        commUtil = new CommonUtil();
        payBean = new PaymentBean();
        userBean = new UserInfoBean();
        userDelegate = new UserInfoDelegate();
        HttpSession loginSession = request.getSession(true);
        Long bankId = 0L;
        boolean isOrderUpdated = false;
        try {
            commUtil.getTopRightListMap(request);
            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
            userBean = userDelegate.getUserInfo(userId, roleId);
            Long paymentId = Long.parseLong(loginSession.getAttribute("paymentId").toString());
            Long paymentType = payDelegate.getPaymentType(paymentId);
            Long cartId = payDelegate.getCartId(userId, 0L);
            payBean = payDelegate.getOrder(userId, paymentId, cartId);
            if (paymentType == 1) {
                DynaActionForm paymentCardForm = (DynaActionForm) form;
                Long cardTypeId = Long.parseLong(paymentCardForm.get("cardTypeId").toString());
                String cardNo = paymentCardForm.get("cardNo").toString();
                String cardHolderName = paymentCardForm.getString("cardHolderName");
                String cardExpiryMonth = paymentCardForm.getString("cardExpiryMonth");
                String cardExpiryYear = paymentCardForm.getString("cardExpiryYear");
                bankId = Long.parseLong(paymentCardForm.getString("bankId"));

                isOrderUpdated = payUtil.savePaymentCard(payBean.getOrderId(), cardTypeId, cardNo, cardHolderName, cardExpiryMonth,
                        cardExpiryYear, bankId, cartId);
            } else if (paymentType == 2) {
                DynaActionForm paymentBankForm = (DynaActionForm) form;
                bankId = Long.parseLong(paymentBankForm.getString("bankId"));
                String bankAccountNo = paymentBankForm.getString("bankAccountNo");
                isOrderUpdated = payUtil.savePaymentBank(payBean.getOrderId(), bankId, bankAccountNo, cartId);

            } else if (paymentType == 3) {
                isOrderUpdated = payDelegate.savePaymentCash(payBean.getOrderId(), cartId);
            }
            if (isOrderUpdated == true) {
                payUtil.sendOrderMail(userBean, payBean);
                request.setAttribute("username", userBean.getFullName());
                SUCCESS = "paymentsuccess";
            } else {
                SUCCESS = "placeordersuccess";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward cancelOrderPayment(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil = new CommonUtil();
        payBean = new PaymentBean();
        payDelegate = new PaymentDelegate();
        HttpSession loginSession = request.getSession(true);
        try {
            commUtil.getTopRightListMap(request);
            Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
            Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
            Long paymentId = Long.parseLong(loginSession.getAttribute("paymentId").toString());
            Long cartId = payDelegate.getCartId(userId, 0L);
            payBean = payDelegate.getOrder(userId, paymentId, cartId);

            boolean isOrderUpdated = payDelegate.cancelOrder(payBean.getOrderId(), cartId);

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }
}
